package com.sdxxtop.robotproject.control;

import android.os.Bundle;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.ainirobot.coreservice.client.RobotApi;
import com.ainirobot.coreservice.client.listener.ActionListener;
import com.ainirobot.coreservice.client.listener.Person;
import com.ainirobot.coreservice.client.listener.PersonInfoListener;
import com.ainirobot.coreservice.client.listener.TextListener;
import com.sdxxtop.robotproject.R;
import com.sdxxtop.robotproject.global.App;
import com.sdxxtop.robotproject.global.Constants;
import com.sdxxtop.robotproject.skill.NavigationSkill;
import com.sdxxtop.robotproject.skill.SpeechSkill;
import com.sdxxtop.robotproject.utils.FuzzyUtils;
import com.sdxxtop.robotproject.utils.MessageParser;

import java.util.ArrayList;
import java.util.List;


/**
 * 处理由ModuleCall回调的消息
 *
 * @author Orion
 * @time 2018/9/14
 */
public class MessageManager {

    private static final String TAG = "MessageManager";
    private List<MessageCallBack> callbackList;

    private MessageManager() {
        callbackList = new ArrayList<>();
    }

    public static MessageManager getInstance() {
        return SingleHolder.INSTANCE;
    }

    private static class SingleHolder {
        public static final MessageManager INSTANCE = new MessageManager();
    }

    public void exeRequest(Message msg) {
        Bundle bundle = msg.getData();
        String type = bundle.getString(Constants.BUNDLE_REQUEST_TYPE);
        String param = bundle.getString(Constants.BUNDLE_REQUEST_PARAM);
        String answerText = MessageParser.getAnswerText(param);
        String userText = MessageParser.getUserText(param);
        switch (type) {
            case Constants.REQUEST_TYPE_SPEECH:
                SpeechSkill.getInstance().setPlaying(false);

                //你们请注意我们的实现逻辑   当喊他的时候  先头转  如果找到人   交点跟随  ，然后身体转动
                // FIXME: 2018/9/20 需要地盘同步转动  //relative absolute
                RobotApi.getInstance().stopFocusFollow(Constants.REQUEST_ID_DEFAULT);
                final Integer angle = Integer.valueOf(param);
                Log.e(TAG, " exeRequest angle :" + angle);
                RobotApi.getInstance().wakeUp(Constants.REQUEST_ID_DEFAULT, angle, new ActionListener() {
                    @Override
                    public void onResult(int status, String responseString) throws RemoteException {
                        super.onResult(status, responseString);
                        Log.e(TAG, "status : " + status + " responseString: " + responseString);
                        RobotApi.getInstance().startSearchPerson(1, 1, "", 2000, new ActionListener() {
                            @Override
                            public void onError(int errorCode, String errorString) throws RemoteException {
                                super.onError(errorCode, errorString);
                                Log.e(TAG, "startSearchPerson onError " + errorCode + ", " + errorString);
                            }

                            @Override
                            public void onResult(int status, String responseString) throws RemoteException {
                                super.onResult(status, responseString);
                                Log.e(TAG, "startSearchPerson onResult " + status + ", " + responseString);
                                switch (status) {
                                    case 1:
                                        RobotApi.getInstance().stopSearchPerson(0);
                                        RobotApi.getInstance().startGetAllPersonInfo(0, new PersonInfoListener() {
                                            @Override
                                            public void onResult(int status, String responseString) {
                                                super.onResult(status, responseString);
                                                Log.e(TAG, "startGetAllPersonInfo status : " + status + " onStatusUpdate: " + responseString);
                                            }

                                            @Override
                                            public void onData(int code, List<Person> data) {
                                                super.onData(code, data);
                                                for (Person datum : data) {
                                                    int id = datum.getId();
                                                    RobotApi.getInstance().startFocusFollow(0, id, 10000, 2, new ActionListener() {
                                                        @Override
                                                        public void onResult(int status, String responseString) throws RemoteException {
                                                            RobotApi.getInstance().stopGetAllPersonInfo(0, null);
                                                            super.onResult(status, responseString);
                                                            Log.e(TAG, "onResult startFocusFollow" + status + ", " + responseString);
                                                        }

                                                        @Override
                                                        public void onError(int errorCode, String errorString) throws RemoteException {
                                                            RobotApi.getInstance().stopGetAllPersonInfo(0, null);
                                                            super.onError(errorCode, errorString);
                                                            Log.e(TAG, "onError startFocusFollow" + errorCode + ", " + errorString);
                                                        }
                                                    });
                                                }
                                                Log.e(TAG, "startGetAllPersonInfo code : " + code + " onStatusUpdate: " + data.toString());
                                            }
                                        });
                                        break;
                                }
                            }
                        });
                    }

                    @Override
                    public void onStatusUpdate(int status, String data) throws RemoteException {
                        super.onStatusUpdate(status, data);
                        Log.e(TAG, "status : " + status + " onStatusUpdate: " + data);
                    }

                    @Override
                    public void onError(int errorCode, String errorString) throws RemoteException {
                        super.onError(errorCode, errorString);
                        Log.e(TAG, "errorCode : " + errorCode + " errorString: " + errorString);
                    }
                });

                break;
            case Constants.REQUEST_TYPE_CRUISE:
                // 巡逻
                break;
            case Constants.REQUEST_TYPE_GUIDE:
                // 导航
                String destination = MessageParser.getDestination(param);
                NavigationSkill.getInstance().startNavigation(destination);
                break;
            case Constants.REQUEST_TYPE_SET_LOCATION:
                // 设点
                String location = MessageParser.getLocation(param);
                NavigationSkill.getInstance().setLocation(location);
                break;
            default:
                break;
        }

//        if (callbackList.size() == 0) {
//            return;
//        }

        if (isPause) {
            return;
        }

        for (MessageCallBack messageCallBack : callbackList) {
            messageCallBack.exeRequest(type, param, answerText);
        }

        // 文字播报
        if (!TextUtils.isEmpty(answerText)) {
            if (!TextUtils.isEmpty(answerText) && answerText.contains("豹小秘")) {
                answerText = answerText.replace("豹小秘", getString(R.string.robot_name));
            }

            if (!TextUtils.isEmpty(answerText) && answerText.contains("小豹")) {
                answerText = answerText.replace("小豹", getString(R.string.robot_name));
            }

            if (!TextUtils.isEmpty(answerText) && answerText.contains("猎豹移动")) {
                answerText = answerText.replace("猎豹移动", getString(R.string.company_name));
            }

            if (!TextUtils.isEmpty(userText) && FuzzyUtils.contains1(userText)) {
                answerText = getString(R.string.report_call);
            }

            if (!TextUtils.isEmpty(userText) && userText.contains("你叫什么名字")) {
                answerText = "我叫小伴，是朱家林田园中合体的，形象代言人。";
            }

            if (!TextUtils.isEmpty(answerText) && answerText.contains("接待机器人")) {
                answerText = "朱家林田园中合体的，形象代言人。";
            }

            if ("打开合影".equals(userText)) {
                return;
            }

            SpeechSkill.getInstance().playTxt(answerText, new TextListener() {
                @Override
                public void onComplete() {
                    super.onComplete();

                    for (MessageCallBack messageCallBack : callbackList) {
                        messageCallBack.onComplete();
                    }
                }
            });
        }
    }

    private boolean isPause;

    public void setPause(boolean pause) {
        isPause = pause;
    }

    public String getString(int stringId) {
        return App.getInstance().getString(stringId);
    }

    public synchronized void addCallBack(MessageCallBack skillCallback) {
        this.callbackList.add(skillCallback);
    }

    public synchronized void removeCallBack(MessageCallBack skillCallback) {
        this.callbackList.remove(skillCallback);
    }

    public void exeCMD(Message message) {
    }

    public void exeHWReport(Message message) {
    }

    public interface MessageCallBack {
        void exeRequest(String type, String param, String answerText);

        void onComplete();
    }
}
